package com.rulai.generator.plugin.terminator;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.mybatis.generator.exception.XMLParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rulai.generator.framework.util.StringUtil;
import com.rulai.generator.plugin.MybatisGeneratorUtil;
import com.rulai.generator.plugin.model.Table2Model;
import com.rulai.generator.plugin.support.VelocityUtil;

import cn.hutool.core.io.FileUtil;

/**
 * 服务层骨架
 * 
 * @since [产品/模块版本]
 * @author leiyang@qding.me
 * @version v1.0 Date:2018年5月9日 上午9:53:03
 */
public class ServiceImplMavenProject extends MavenSkeletonProject {

    private static final Logger LOG = LoggerFactory.getLogger(ServiceImplMavenProject.class);

    /**
     * tables 表模型
     */
    private List<Table2Model> tables;

    /**
     * module 模块名
     */
    private String module;

    /**
     * packageName 包名
     */
    private String packageName;


    /**
     * serviceImpl_vm  ServiceImpl模板路径
     */
    private static String serviceImpl_vm = "/template/ServiceImpl.vm";

    public ServiceImplMavenProject(String projectPath, boolean repeatDel, List<Table2Model> tables, String packageName,String module) {
        super(projectPath, repeatDel);

        this.tables = tables;
        
        this.packageName = packageName;
        
        this.module=module;
        
        this.setArtifactId(module + "-service");
    }
    
    
    private String getServiceImplPath() {
        
        if (MavenSkeletonProject.osInfo.isWindows()) {
            return this.getMainJavaPath() +"/"+ packageName.replaceAll("\\.", "/") + "/" + "service/impl";
        } else {
            return this.getMainJavaPath() +"/"+ packageName.replaceAll("\\.", "/") + "/" + "service/impl";
        }

//        return this.getMainJavaPath() + packageName.replaceAll("\\.", MavenSkeletonProject.osInfo.getFileSeparator())
//                +MavenSkeletonProject.osInfo.getFileSeparator()+"impl";
    }

    /**
     * 填充项目内容 <功能详细描述>
     * 
     * @author leiyang@qding.me Date:2018年5月9日 下午2:19:13
     * @throws XMLParserException
     * @throws IOException
     */
    @Override
    protected void fillingContent() throws Exception {

        LOG.info("========== 开始生成ServiceImpl  ==========");

        if (osInfo.isWindows()) {
            serviceImpl_vm = MybatisGeneratorUtil.class.getResource(serviceImpl_vm).getPath().replaceFirst("/", "");
        } else {
            serviceImpl_vm = MybatisGeneratorUtil.class.getResource(serviceImpl_vm).getPath();
        }

        String ctime = new SimpleDateFormat("yyyy/M/d").format(new Date());
        String serviceImplPath = getServiceImplPath();
        if(!FileUtil.exist(serviceImplPath)){
            //创建dir
            FileUtil.mkdir(serviceImplPath);
        }
        for (int i = 0; i < tables.size(); i++) {
            String model = tables.get(i).getModelName();
            // 生成serviceImpl
            File serviceImplFile= FileUtil.file(serviceImplPath, model + "ServiceImpl.java");
            if (!serviceImplFile.exists()) {
                VelocityContext context = new VelocityContext();
                context.put("package_name", packageName);
                context.put("model", model);
                context.put("mapper", StringUtil.toLowerCaseFirstOne(model));
                context.put("ctime", ctime);
                VelocityUtil.generate(serviceImpl_vm, serviceImplFile.getAbsolutePath(), context);
                System.out.println(serviceImplFile.getAbsolutePath());
            }
            
        }

        LOG.info("========== 结束生成ServiceImpl ==========");

    }

}
